<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="统计周期" prop="queryParams.hourLimit">
        <el-radio :label="24" v-model='queryParams.hourLimit'>
          24小时
        </el-radio>
        <el-radio :label="72" v-model='queryParams.hourLimit'>
          72小时
        </el-radio>
        <el-radio :label="168" v-model='queryParams.hourLimit'>
          7天
        </el-radio>
        <el-radio :label="720" v-model='queryParams.hourLimit'>
          30天
        </el-radio>
      </el-form-item>
      <el-form-item label="归属部门" prop="queryParams.deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门" width="200px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:gasClientDataStatistics:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="exportExcel"
          v-hasPermi="['device:gasClientDataStatistics:export']"
        >导出2</el-button>
      </el-col>
    </el-row>

    <el-table ref="dataTable" v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column label="燃气设备24小时上报数据统计" header-align="center">
        <el-table-column prop="clientNo" label="设备编号">
        </el-table-column>
        <el-table-column prop="deptName" label="归属单位">
        </el-table-column>
        <el-table-column prop="vbat" label="电池电压(V)">
        </el-table-column>
        <el-table-column prop="eventCount" label="报警事件(次)">
        </el-table-column>
        <el-table-column prop="originCount" label="原始数据(次)">
        </el-table-column>
        <el-table-column prop="lastTime" label="最后通信时间">
        </el-table-column>
        <el-table-column prop="wakeCount" label="唤醒次数">
        </el-table-column>
      </el-table-column>
    </el-table>
  </div>
</template>

<style>
/* Treeselect */
.vue-treeselect {
  width: 230px;
  line-height: 30px;
}

</style>

<script>
import { gasClientDataStatistics } from "@/api/device/gasStatistics";
import {listDept} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import FileSaver from 'file-saver'
import * as XLSX from 'xlsx'


export default {
  name: "GasClientDataStatistics",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 燃气监测设备表格数据
      tableData: [],
      // 部门树选项
      deptOptions: [],
      // 查询参数
      queryParams: {
        hourLimit: 24,
        deptId: null
      }
    };
  },
  created() {
    this.getDeptList();
    this.getList();
  },
  methods: {
    getDeptList(){
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, "deptId");
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    /** 查询燃气监测设备列表 */
    getList() {
      this.loading = true;
      gasClientDataStatistics(this.queryParams).then(response => {
        this.tableData = response.data;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('visual/statistics/gasClientDataStatistics/export', {
        ...this.queryParams
      }, `管道监测设备数据统计_${new Date().getTime()}.xlsx`)
    },
    exportExcel() {
      try {
        const $e = this.$refs['dataTable'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        console.log(XLSX)
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })
        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `管道监测设备数据统计_${new Date().getTime()}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }
  }
};
</script>
